#!/usr/bin/env python
# Copyright(c) 2010 Nuttachat Wisittipanit. All rights reserved.

from mahbo.information.database.utility.dbinterface import DatabaseInterface


class InfoManage(DatabaseInterface):
    
    def __init__(self,**kwargs):        
        self.mainDBname = 'gutmtps'
        self.__dict__.update(kwargs)
        DatabaseInterface.__init__(self,self.mainDBname,True)
        self.addConnect('gut')
        
    def getGutLHPCRdata(self):
        #Get SampleName from table gut from gut database
        self.adb['gut'].query("""select %s from %s
                              """ % ('m$Sample_Name','gut'))
        row                 = self.adb['gut'].cursor.fetchall()
        #.rSName -> The entire list of gut sample names
        self.rSName         = [u['m$Sample_Name'] for u in row]
        self.userSName      = [] #[('Dys100_37_PCR2_103_61','Dys100_37'),..]
        self.cuserSName     = [] #['Dys100_37','Dys100_19','PG3_45',..]
        #lhpcrSampleName has a weird name, ex. 'Dys100_37_PCR2_103_61' means 'Dys100_37' 
        for i in self.rSName:            
            #i -> 'Dys100_37_PCR2_103_61' or 'PG_03_PS_0_15'
            a = i.find('_')
            if a == -1:
                continue
            b = i.find('_',a+1)
            if b == -1:
                #if i -> 'PG3_45', usename = 'PG3_45'
                usename = i
            else:
                #if i -> 'Dys100_37_PCR2_103_61', usename = 'Dys100_37'
                usename = i[:b]
            try:
                self.cuserSName.index(usename)
            except (ValueError,):
                self.cuserSName.append(usename)
                self.userSName.append((i,usename))
                
    def getGutMTPSdata(self):
        self.v8tb        = 'rdp8level1'
        self.v10tb       = 'rdp10level1'
        #Get sample names from level 8
        self.RD.query("""select %s from %s
                      """ % ('m$SampleName',self.v8tb))                        
        row              = self.RD.cursor.fetchall()
        self.v8SName     = [u['m$SampleName'] for u in row]
        #Get sample names from level 10
        self.RD.query("""select %s from %s 
                      """ % ('m$SampleName',self.v10tb))
        row              = self.RD.cursor.fetchall()
        self.v10SName    = [u['m$SampleName'] for u in row]
        
    def findMutualSName(self):
        self.getGutLHPCRdata()
        self.getGutMTPSdata()
        #So I have 
        #- cuserSName
        #- v8SName
        #- v10SName
        self.cuserSName_set = set(self.cuserSName)
        self.v8SName_set    = set(self.v8SName)
        self.v10SName_set   = set(self.v10SName)
        self.mutualSName    = list(self.cuserSName_set & self.v8SName_set & self.v10SName_set)
        self.mutualSName.sort()
        print self.v10SName_set - self.v8SName_set
        print self.v8SName_set - self.v10SName_set
#        self.recordMutualSName()
        
    def recordMutualSName(self):
        for i in self.mutualSName:
            self.RT.query("""insert into %s (%s) values  ("%s")
                          """ % (self.RT.tab_ms.tablename   ,    
                                 self.RT.tab_ms.sname       ,
                                 i                          ,))
    
    def getMutualSName(self):
        self.RT.query("""select %s from %s
                      """ % (self.RT.tab_ms.sname       ,    
                             self.RT.tab_ms.tablename   ,))
        row         = self.RT.cursor.fetchall()
        mutualSName = [u[self.RT.tab_ms.sname] for u in row]
        return mutualSName
                                 
        
    
if __name__ == '__main__':
    ifm = InfoManage()
    ifm.findMutualSName()
    print ifm.mutualSName
    print len(ifm.mutualSName)
    